//
//  MemberActivityCell.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/12.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "MemberActivityCell.h"
#import "UIImageView+WebCache.h"
@interface MemberActivityCell()

@property (strong, nonatomic)  UILabel *lineLabel;
@property (strong, nonatomic)  UIImageView *imageUrlFull;
@property (strong, nonatomic)  UILabel *actName;
@property (strong, nonatomic)  UILabel *joinTime;
@property (strong, nonatomic)  UILabel *sat;

@end
@implementation MemberActivityCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //self.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
        
        [self createView];
        [self setViewAutoLayout];
    }
    
    return self;
    
}
-(void)createView
{
    self.imageUrlFull=[[UIImageView alloc] init];
    [self addSubview:self.imageUrlFull];
   
    UIView*bianxian=[[UIView alloc]initWithFrame:CGRectMake(8, 8, ScreenWidth-16,200-8)];
    [bianxian.layer setCornerRadius:0.5];
    [bianxian.layer setBorderWidth:0.5];
    [bianxian.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){204.0/255.0, 204.0/255.0, 204.0/255.0, 1.0  });
    [bianxian.layer setBorderColor:colorref2];//边框颜色
    [self addSubview:bianxian];

    self.actName=[[UILabel alloc] init];
    self.actName.font= [UIFont systemFontOfSize:14.0];
    self.actName.textColor=[UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0];
    [self addSubview:self.actName];
    
    self.joinTime=[[UILabel alloc] init];
    self.joinTime.font= [UIFont systemFontOfSize:14.0];
    self.joinTime.textColor=zhonghuiColor;
    [self addSubview:self.joinTime];
    
    self.sat=[[UILabel alloc] init];
    self.sat.font= [UIFont systemFontOfSize:14.0];
    [self addSubview:self.sat];
    

    
}
-(void)setModel:(MemberActivityModel *)model
{
    
    [self.imageUrlFull setImageWithURL:[NSURL URLWithString:model.imageUrlFull] placeholderImage:[UIImage imageNamed:@"changfangxing"]];
    self.actName.text=[NSString stringWithFormat:@"%@",model.actName];
    
    if (model.joinTime .length>0) {
        
        
        self.joinTime.text=[NSString stringWithFormat:@"%@",model.joinTime];
        NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithString:model.joinTime];
        
        [AttributedStr2 addAttribute:NSFontAttributeName
         
                               value:[UIFont systemFontOfSize:14.0]
         
                               range:NSMakeRange(0, 4)];
        
        [AttributedStr2 addAttribute:NSForegroundColorAttributeName
         
                               value:zhonghuiColor
         
                               range:NSMakeRange(0, 4)];
        
        
        self.joinTime.attributedText = AttributedStr2;
        
    }else{

    
    
    
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    
    NSString* string = model.endDate;
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate= [dateFormatter1 dateFromString:string];
    
    
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDateComponents *d = [calendar components:unitFlags fromDate: [NSDate date] toDate:destDate options:0];//计算时间差
    
    
    NSString* string2 = model.endDate;
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    NSDate *destDate2= [dateFormatter2 dateFromString:string2];
    NSTimeInterval  timeInterval2 = [destDate2 timeIntervalSinceDate:[NSDate date]];
    
    
    if ([d day] <=0) {
        
        if ([d month] <=0) {
            self.joinTime.text = [NSString stringWithFormat:@"剩%@天%ld小时   结束",@"0", (long)[d hour]];
            
            self.joinTime.textColor = [UIColor colorWithRed:204.0/255.0 green:3.0/255.0 blue:2.0/255.0 alpha:1.0];
//            NSString*str=[NSString stringWithFormat:@"%@", self.joinTime.text];

            NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"剩%@天%ld小时   结束",@"0", (long)[d hour]]];
            
            [AttributedStr addAttribute:NSFontAttributeName
             
                                  value:[UIFont systemFontOfSize:14]
             
                                  range:NSMakeRange(0, self.joinTime.text.length)];
            
            [AttributedStr addAttribute:NSForegroundColorAttributeName
             
                                  value:shenhuiColor
             
                                  range:NSMakeRange(0, 9 )];
            
            
            self.joinTime.attributedText = AttributedStr;
        }
        
        if ([d second] <0) {
            self.joinTime.text=@"已结束";
            
        }
        
        
        
        
    }else{
        
        if ([d month] <=0) {
            self.joinTime.text = [NSString stringWithFormat:@"剩%ld天%ld小时  结束", (long)[d day], (long)[d hour]];
        }
        
        if ([d second] <0) {
            self.joinTime.text=model.endDate;
            
        }
        
        self.joinTime.textColor = [UIColor colorWithRed:204.0/255.0 green:3.0/255.0 blue:2.0/255.0 alpha:1.0];
//        NSString*str=[NSString stringWithFormat:@"%@", self.joinTime.text];
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"剩%.0f天%ld小时   结束",timeInterval2/60/60/24-1, (long)[d hour]]];
        
        [AttributedStr addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:14]
         
                              range:NSMakeRange(0, self.joinTime.text.length)];
        
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:shenhuiColor
         
                              range:NSMakeRange(0, 9)];
        
        
        self.joinTime.attributedText = AttributedStr;
        
    }

    }
    
}


-(void)setViewAutoLayout
{
    
    [self.imageUrlFull mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(15) ;
        make.right.equalTo(self.mas_right).with.offset(-15) ;
        make.height.equalTo(@150);
        make.top.equalTo(self.mas_top).with.offset(15);
    }];

    [self.actName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(15) ;
        make.height.equalTo(@15);
        make.top.equalTo(self.imageUrlFull.mas_bottom).with.offset(10);
    }];
    
    
    [self.joinTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-15) ;
        make.height.equalTo(@14);
        make.centerY.equalTo(self.actName.mas_centerY) ;
    }];

    
    
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
